package com.cardvalue.sys.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.cardvalue.sys.R;
import com.cardvalue.sys.base.BaseActivity;
import com.cardvalue.sys.cache.LocalCache;
import com.cardvalue.sys.code.CallBackCode;
import com.cardvalue.sys.entity.Message;
import com.cardvalue.sys.entity.User;
import com.cardvalue.sys.fragment.BasicFragment;
import com.cardvalue.sys.mode.UserService;
import com.cardvalue.sys.tool.Convert;
import com.cardvalue.sys.tool.IdCard;
import com.cardvalue.sys.tool.MessageBox;
import com.cardvalue.sys.tool.Utiltools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;
import io.realm.RealmResults;

/**
 * 手机验证
 */
public class IsJxlValidActivity extends BaseActivity {

    @Bind(R.id.user_name) EditText user_name;//姓名
    @Bind(R.id.user_dentity)  EditText user_dentity;//身份证号
    @Bind(R.id.user_number) EditText user_number;//手机号码
    @Bind(R.id.user_password)  EditText user_password;//运营商服务密码
    @Bind(R.id.user_code)  EditText user_code;//运营商验证码
    @Bind(R.id.ht_code)  LinearLayout ht_code;
    @Bind(R.id.user_submit) Button user_submit;//提交的按钮

    private Realm realm;
    private UserService updateInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_isjxl_valid);
        //手机验证
        setHeaderFields(0, R.string.phone_verification,0,R.mipmap.back, 0,0);
        dialog=new ProgressDialog(this,R.style.dialog);
        ButterKnife.bind(this);
        dialog=new ProgressDialog(this,R.style.dialog);
        realm=Realm.getDefaultInstance();
        updateInfo=new UserService(this);
        initData();//设置数据
    }


    @OnClick(R.id.user_submit) void SubmitClick(){
        if(!IdCard.IDCardValidate(user_dentity.getText().toString().trim())){
            MessageBox.ToastShow("身份证填写有误", IsJxlValidActivity.this);
            return;
        }else{
            MessageBox.show(dialog, "","提交中...可能需要等待一会，请不要退出此页面！");
            String ownerName=user_name.getText().toString();
            String ownerSSN=user_dentity.getText().toString();
            String mobilePhone=user_number.getText().toString();
            String password=user_password.getText().toString();
            Utiltools.printE("ht_code.getVisibility============",ht_code.getVisibility()+"");
            //返回值为0，visible；返回值为4，invisible；返回值为8，gone
            if(ht_code.getVisibility()==View.GONE){
                updateInfo.jxlGetVerityCode(ownerName, ownerSSN, mobilePhone, password, new UserService.GetjxlGetVerityCodeCallback() {
                    @Override
                    public void success(User user) {
                        dialog.cancel();
                        if(Convert.convertToString(user.getProcessCode()).equals("10002")){
                            ht_code.setVisibility(View.VISIBLE);
                            MessageBox.ToastShow("请输入验证码", IsJxlValidActivity.this);
                            return;
                        }else if(Convert.convertToString(user.getProcessCode()).equals("10008")){//跟新用户信息
                            getUserInfo();
                            return;
                        }else{
                            MessageBox.ToastShow(user.getContent().toString(), IsJxlValidActivity.this);
                            ht_code.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void fail(User user) {

                    }

                    @Override
                    public void cancelDialog() {
                        dialog.cancel();
                    }
                });//聚信力第一步

                return;
            }else if(ht_code.getVisibility()==View.VISIBLE){
                dialog.cancel();
                Utiltools.printE("请输入验证码====","请输入验证码====");
                String temp=user_code.getText().toString();
                if(temp.length()<3){

                    MessageBox.ToastShow("请输入验证码", IsJxlValidActivity.this);
                    return;
                }
                updateInfo.jxlGetJxlSubmitCode(temp,user_number.getText().toString(),user_password.getText().toString(), new UserService.GetjxlGetVerityCodeCallback() {//聚信力第二步
                    @Override
                    public void success(User user) {
                        if(Convert.convertToString(user.getProcessCode()).equals("10008")){//跟新用户信息
                            getUserInfo();
                        }else{
                            MessageBox.ToastShow( user.getContent().toString(),IsJxlValidActivity.this);
                            ht_code.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void fail(User user) {

                    }

                    @Override
                    public void cancelDialog() {

                    }
                });
            }


        }
    }
    /**
     * 设置值
     */
    public void initData(){
        RealmResults<User> results2 = realm.where(User.class).findAll();
        User results1 =realm.where(User.class).equalTo("mobilePhone", LocalCache.getStringLocalData(IsJxlValidActivity.this,"mobilePhone")).findFirst();
        if(TextUtils.isEmpty(results2.size()+"")){
            return;
        }
        user_name.setText(results1.getOwnerName());
        user_dentity.setText(results1.getOwnerSSN());
        ht_code.setVisibility(View.GONE);
    }


    public void getUserInfo(){
        //还要更新用户（还没有写）
        //发消息跟新手机验证
        android.os.Message msg=new android.os.Message();
        msg.what= CallBackCode.CALLBACK_MSG_GETISJXIVALID;//手机验证
        BasicFragment.handler.sendMessage(msg);
        IsJxlValidActivity.this.finish();
    }
}
